<?php
     namespace Dashboard\Controller;
     use Think\Controller;
     header("Content-type: text/html; charset=utf-8");
     class BaseController extends Controller{
     	public function _initialize(){
            $name = "医美";
            $this->assign("nn",$name);
     	    //左侧列表样式
     	    $active_id = I('active_id');
            if($active_id){
                $_SESSION['active_id'] = $active_id;
                $this->assign("active_id",$active_id);
            }else{
                if(!$_SESSION['active_id']){
                    $this->assign("active_id",1);
                }else{
                    $this->assign("active_id",$_SESSION['active_id']);
                }
            }
     	    //测试代码
     		if($_SESSION['admin']['id'] == null){
     			$this->success("您还没有登录，无权访问该页面",U('Login/index'));
     			exit();
     		}else{
                $id           = $_SESSION['admin']['id'];
                $temp         = sel("admin","",array("id"=>$id));
                if($temp['status'] == 1){
                    //普通管理员
                    if($temp['type'] == 1){
                        define('QUAN',1);
                    }
                    //超级管理员
                    if($temp['type'] == 2){
                        define('QUAN',2); //超级管理员
                    }
                    //商家
                    if($temp['type'] == 3){
                        define('QUAN',3);
                    }
                    $_SESSION['admin'] = $temp;
                }else{
                    session_destroy();
                    $this->success("您的账号已被禁用，即将退出后台！",U('Login/index'));
                }
            }
     	}
     	public function out(){
            session_destroy();
            $this->success("退出成功！！",U('Login/index'));
     	}
         //单图上传
         function imgUpload($url)
         {
             $config = array(
                 'maxSize' => 314572800000000,
                 'rootPath' => './Data/',
                 'savePath' => $url,
                 'saveName' => time() . '_' . mt_rand(),
                 'exts' => array('jpg', 'png', 'jpeg', 'gif','mp4','mp3'),
                 'autoSub' => true,
                 'subName' => array('date', 'Ymd')
             );
             $upload = new \Think\Upload($config);
             $info = $upload->upload();
             if (!$info) {
                 $this->error("图片上传失败",$upload->getError());
             }
             return $info;
         }

         //音乐上传
         function musicUpload()
         {
             $config = array(
                 'maxSize' => 83886908,
                 'rootPath' => 'Music/',
                 'savePath' => 'upload/',
                 'saveName' => time() . '_' . mt_rand(),
                 'exts' => array('mp3','mp4','jpg', 'png', 'jpeg', 'gif'),
                 'autoSub' => true,
                 'subName' => array('date', 'Ymd')
             );
             $upload = new \Think\Upload($config);
             $info = $upload->upload();
             if (!$info) {
                 $this->error("图片上传失败",$upload->getError());
             }
             return $info;
         }
         //多图上传
         public function imgUp(){
             $upload = new \Think\Upload();
             $upload->maxSize   = 8388608;
             $upload->exts      = array('jpg', 'png', 'jpeg','gif');
             $upload->rootPath  = 'Data/';
             $upload->savePath  = "course/";
             $upload->autoSub   = true;
             $upload->subName   = array('date','Y-m');

             $info   =   $upload->upload();
             foreach($info as $v){
                 $url['info'][]    = 'Data/'.$v['savepath'].$v['savename'];
                 $this->thumbImg('Data/'.$v['savepath'].$v['savename'],'Data/'.$v['savepath'].'s_'.$v['savename']);
                 $url['thumb'][]   = 'Data/'.$v['savepath'].'s_'.$v['savename'];
             }
             return $url;
         }
         //多图上传
         public function imgUps($urls){
             $upload = new \Think\Upload();
             $upload->maxSize   = 8388608;
             $upload->exts      = array('jpg', 'png', 'jpeg','gif');
             $upload->rootPath  = 'Data/';
             $upload->savePath  = $urls;
             $upload->autoSub   = true;
             $upload->subName   = array('date','Y-m');

             $info   =   $upload->upload();
             foreach($info as $v){
                 $url['info'][]    = 'Data/'.$v['savepath'].$v['savename'];
                 $this->thumbImg('Data/'.$v['savepath'].$v['savename'],'Data/'.$v['savepath'].'s_'.$v['savename']);
                 $url['thumb'][]   = 'Data/'.$v['savepath'].'s_'.$v['savename'];
             }
             return $url;
         }
         //生成缩略图
         protected function thumbImg($path,$thumbName){
             $image = new \Think\Image();
             $image->open($path);
             $getWidth = $image->width();
             $getHeight= $image->height();

             if ($getWidth <= $getHeight){
                 $width = $getWidth;
                 // if($width >= $width_t){$width = $width_t;}
                 $image->thumb(\Think\Image::IMAGE_THUMB_CENTER)->save($thumbName);
             }elseif($getWidth >= $getHeight){

                 $height = $getHeight;
                 //if($height >= $width_t){$width = $width_t;}
                 $image->thumb(\Think\Image::IMAGE_THUMB_CENTER)->save($thumbName);
             }

         }
     }